package com.taptech.doufu.ui.view.drawcircle;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.facebook.drawee.view.SimpleDraweeView;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NovelDetailsActivity;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.bean.MineNovelBean;
import com.taptech.doufu.bean.TagsBean;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.ScreenUtil;

/**
 * 排行页面小说数据适配器
 */
public class NovelRankMainViewHolder extends BaseRecyclerViewViewHolder implements View.OnClickListener {

    Context mContext;

    public TextView novelTitle;
    public TextView novelAuthor;
    public TextView novelDes;
    private TextView tag1;
    private TextView tag2;
    public TagsBean[] titleLabels;
    private ImageView novelImage;
    private RelativeLayout novleMainlayout;

    MineNovelBean bean;

    private String novelRankUrl;


    public NovelRankMainViewHolder(Context context, View convertView) {
        super(context, convertView);
        mContext = context;

        novleMainlayout = (RelativeLayout) convertView.findViewById(R.id.novel_rank_main_layout);
        novelTitle = (TextView) convertView.findViewById(R.id.home_novel_item_title);
        novelAuthor = (TextView) convertView.findViewById(R.id.home_novel_item_author);
        novelDes = (TextView) convertView.findViewById(R.id.home_novel_item_des);
        novelImage =  convertView.findViewById(R.id.novel_rank_novel_image);
        tag1 = (TextView) convertView.findViewById(R.id.novel_rank_main_tag1);
        tag2 = (TextView) convertView.findViewById(R.id.novel_rank_main_tag2);

        novleMainlayout.setOnClickListener(this);
    }

    @Override

    public void setViewHolderChildViewContent(MineAbstractBean tempbean, int position) {
//
        if (position == 0) {
            novleMainlayout.setPadding(0, ScreenUtil.dip2px(mContext,8),ScreenUtil.dip2px(mContext,12),ScreenUtil.dip2px(mContext,16));
        }else{
            novleMainlayout.setPadding(0, 0,ScreenUtil.dip2px(mContext,12),ScreenUtil.dip2px(mContext,16));
        }
        bean = (MineNovelBean) tempbean;
        mPosition = position;
        //set tag info
        if (bean.getTags() != null && bean.getTags().length > 0) {
            titleLabels = bean.getTags();
            if (titleLabels.length > 1 && titleLabels[0] != null) {
                tag1.setText(titleLabels[0].getName());
                tag1.setVisibility(View.VISIBLE);
            }
            if (titleLabels.length > 2 && titleLabels[1] != null) {
                tag2.setText(titleLabels[1].getName());
                tag2.setVisibility(View.VISIBLE);
            }
        } else {
            tag1.setVisibility(View.GONE);
            tag2.setVisibility(View.GONE);
        }


        //set novel title
        novelTitle.setText(bean.getTitle());
        //set novel image
        if (bean.getImages() != null && bean.getImages().length > 0) {
            if ((bean.getImages()[0].getImgUrl() != null) && (bean.getImages()[0].getImgUrl().length() > 0)) {
                GlideUtil.displayRoundImage(mContext, novelImage, bean.getImages()[0].getImgUrl(), 3, R.drawable.img_default_loading_l);
            }
        }
        // set author
        if (bean.getAuthor() != null && !bean.getAuthor().equals("")) {
            novelAuthor.setText(bean.getAuthor());
        } else {
            novelAuthor.setText("佚名");
        }

        // set des or show ticket
        if (bean.getMonth_ticket_sum() > 0) {
            String ticket_info = "  <font color=#ff606e>" + bean.getMonth_ticket_sum() + "</font>";
            novelDes.setText(Html.fromHtml(ticket_info + "   月票"));
        }else {
            novelDes.setText(bean.getDes());
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.novel_rank_main_layout:
                if (bean != null) {
                    NovelDetailsActivity.Companion.startActivity(mContext,bean.getId(),Intent.FLAG_ACTIVITY_NEW_TASK);
                }
                break;
            default:
                break;
        }
    }
}
